//
//  AliPayTool.m
//  CommunityNannyO2O
//
//  Created by 王禹丞 on 15/9/17.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "AliPayTool.h"
#import <AlipaySDK/AlipaySDK.h>
#import "Order.h"
#import "DataSigner.h"
#import "AccountTool.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"

@implementation AliPayTool

+(void)alipayWithOrdercode :(NSString *)ordercode orderId:(NSString *)orderid Price :(NSString *)price CityId:(NSString *)cityId Target:(UIViewController *)target
{
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Alipay" ofType:@"plist"];
    
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    
    NSString *partner = data[@"partner"];
    
    NSString *seller = data[@"seller"];
    
    NSString *privateKey = data[@"privateKey"];
    
    Order *order = [[Order alloc] init];
    
    order.partner = partner;
    
    order.seller = seller;
    
    order.tradeNO = orderid; //订单ID（由商家自行制定）
    
    order.productName = @"迷笛"; //商品标题
    
    order.productDescription = @"迷笛"; //商品描述
    
    order.amount = [NSString stringWithFormat:@"%.2f",price.floatValue]; //商品价格
    
    order.notifyURL =  @"http://www.baidu.com"; //回调URL
    
    order.service = @"mobile.securitypay.pay";
    
    order.paymentType = @"1";
    
    order.inputCharset = @"utf-8";
    
    order.itBPay = @"30m";
    
    order.showUrl = @"m.alipay.com";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
    NSString *appScheme = @"MIDI";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    
    NSLog(@"orderSpec = %@",orderSpec);
    
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    
    id<DataSigner> signer = CreateRSADataSigner(privateKey);
    
    NSString *signedString = [signer signString:orderSpec];
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    
    if (signedString != nil) {
        
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        [AccountTool saveOrderId:orderid];
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
           
            [AliPayTool processResult:resultDic Target:target];
        }];
    }
}

+(void)processResult:(NSDictionary *)resultDic Target:(id)target
{
    NSString *flag = @"";
    
    if ([resultDic[@"resultStatus"] isEqualToString:@"9000"] )
    {
        [MBProgressHUD showSuccess:@"支付成功"];
        
        QXNetWorkTool * tool = [[QXNetWorkTool alloc]init];
        
        tool.QXNetWorkDelegate = target;
        
        flag = @"1";
        
        [AccountTool savePayResult:VPaySuccess];
    }
    else
    {
        [MBProgressHUD showSuccess:@"支付失败"];
        
        QXNetWorkTool * tool = [[QXNetWorkTool alloc]init];
        
        tool.QXNetWorkDelegate = target;
        
        flag = @"0";
        
        [AccountTool savePayResult:VPayFail];
    }
    
    //发消息
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *netTools = [[QXNetWorkTool alloc]init];
    netTools.QXNetWorkDelegate = target;
    
    NSString *payItem = [AccountTool takePayItem];
    
    if ([payItem isEqualToString:VPayBuyMusic])
    {
        //记录购买结果
        [netTools GetDataFromServerWithPostMethodandInterfaceName:MyOrderBuyRecorderInterface andParameter:
         @[@{
               @"order_id":[AccountTool takeOrderId],
               @"flag":flag
               }] tag:6666];
    }
    else if ([payItem isEqualToString:VPaySignCost])
    {
        //支付报名费付款
        [netTools GetDataFromServerWithPostMethodandInterfaceName:PaySiginUpCostInterface andParameter:
         @[@{@"exam_id": [AccountTool takeExamId],
             @"flag":flag}] tag:7777];
    }
    else if ([payItem isEqualToString:VPayCerficationCost])
    {
        //支付证书工本费
        [netTools GetDataFromServerWithPostMethodandInterfaceName:PayCertificateCostInterface andParameter:
         @[@{@"exam_id":[AccountTool takeExamId],
             @"status":flag,                                          @"courier_cost":[AccountTool takeOrderPrice]}] tag:8888];
    }
    
    if ([target respondsToSelector:@selector(payResultOtherProcess:)]) {
        
        [target payResultOtherProcess:resultDic];
    }
}


@end























